﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DynamicExpresso;
using Newtonsoft.Json.Linq;
using ST.Core;



namespace ST.Core.Util
{
    public class Calc
    {
         

       
       
        public UInt16 CalcUInt16(string fml ,List<UInt16Value> l)
        {
            var interpreter=new Interpreter();  
            foreach (var m in l)
            {
                interpreter.SetVariable(m.Name,m.Id);
            }
            var d = interpreter.Eval<UInt16>(fml);
            return d;
        }

        public float CalcFloat(string fml, List<UInt16Value> l)
        {
            var interpreter = new Interpreter();
            foreach (var m in l)
            {
                interpreter.SetVariable(m.Name, m.Id);
            }
            var d = interpreter.Eval<float>(fml);
            return d;
        }
        public decimal CalcDecimal(string fml, List<UInt16Value> l)
        {
            var  v=CalcFloat(fml, l);
            return Convert.ToDecimal(v);
        }


       public decimal CalcDecimalArgs(string fml, params UInt16Value[] args)
        {
            var l = args.ToList();
            var v = CalcFloat(fml, l);
            return Convert.ToDecimal(v);
        }
        public UInt16 CalcUInt16Args(string fml, params UInt16Value[] args)
        {
            var l = args.ToList();
            var v = CalcUInt16(fml, l);
            return v;
        }
        /// <summary>
        /// 获取byte的指定bit位的值
        /// </summary>
        /// <param name="b">字节</param>
        /// <param name="index">bit位索引（从1开始）</param>
        /// <returns>bit位的值（0或1）</returns>
        public int BitValue(byte b, int index)
        {
            if (index < 1 || index > 8)
                throw new ArgumentOutOfRangeException(nameof(index));

            return (b >> (index - 1)) & 0x01;
        }


         
         
    }
}
